#include "DebugData.h"
#include "string.h"

static int done_callback2(ProtolMachine *pack_serial)
{
#if 0
	int i = 0;
	printf("=========complete server package========\n");
	for (i = 0; i < pack_serial->package_length; i ++) {
		printf ("%x ", pack_serial->package_content[i]);
	}
	printf ("\n=======complete package======\n");

	if(pack_serial->package_content[4] == 0x23)
	{
		printf ("\n=======Milk Data======\n");
	}
#endif	
	return 0;
}

void UploadDebugDataToSvr(uint8_t eChNum, uint8_t u8Station)
{
#if 0
	if(eChNum >= CHANNEL_CNT || u8Station >= STATION_CNT)
	{
		return;
	}
	ReportFrameMilk tReportFrameMilk = 
	{
		.RepHeader = {0x55AA, 0x13, 0x23},
		.RepEnder = {0x0000, 0x81AA}
	};
	
	memcpy(tReportFrameMilk.RepData.RanchNum, gtDeviceInfo.u8aRanchNum, 4);
	
	memcpy(tReportFrameMilk.RepData.MlikVal, 
				g_tMilkData.tMilkVal[eChNum].tReportMilkData[u8Station].MilkVal,
					sizeof(tReportFrameMilk.RepData.MlikVal));
	
	memcpy(tReportFrameMilk.RepData.ElecVal, 
				g_tMilkData.tMilkVal[eChNum].tReportMilkData[u8Station].ElecVal,
					sizeof(tReportFrameMilk.RepData.ElecVal));
	
	tReportFrameMilk.RepData.StationNum = 
					g_tMilkData.tMilkVal[eChNum].tReportMilkData[u8Station].StationNum;
	
	tReportFrameMilk.RepData.TimeStamp = 
					g_tMilkData.tMilkVal[eChNum].u32MilkDataCurrTimeStamp;
	
	tReportFrameMilk.RepData.BatchNum = 
		g_tMilkData.tMilkVal[eChNum].tBatchNumData.u8MilkCurr_BatchNum;
	
	tReportFrameMilk.RepData.DevNum = gtDeviceInfo.u8CPUIDEncry;
	
	tReportFrameMilk.RepEnder.CrcVal = u16MBCRC16((uint8_t *)&tReportFrameMilk.RepData,
														sizeof(tReportFrameMilk.RepData));
	
	if(GetNetworkStatus())
	{
		SendToServer((uint8_t*)&tReportFrameMilk, sizeof(tReportFrameMilk));
	}
#endif
}

static int cmd_milkdata_callback(void *content, int data_length)
{
#if 0
	WNUFrameMilk tWNUFrameMilk = {0};
	eChannelNum eChnlNum = eChannel1;
	uint8_t u8StationNum = 0;
	uint8_t u8TmpStationNum = 0;
	
	memcpy(&tWNUFrameMilk, content, data_length);
	u8StationNum = tWNUFrameMilk.WMilkData.MilkPointID;
	u8StationNum -= 1;
	
	eChnlNum = GetChannelNum(u8StationNum);
	
	u8TmpStationNum = u8StationNum % STATION_CNT;
	
	if(!CheckMilkGetDataStatus(eChnlNum))
	{
		SetMilkDataOcurr(eChnlNum);
		g_tMilkData.tMilkVal[(uint8_t)eChnlNum].tBatchNumData.u8MilkCurr_BatchNum ++;
	}
	
	memcpy(g_tMilkData.tMilkVal[eChnlNum].
				tReportMilkData[u8TmpStationNum].MilkVal,
					tWNUFrameMilk.WMilkData.Yield,
					sizeof(tWNUFrameMilk.WMilkData.Yield));
	
	memcpy(g_tMilkData.tMilkVal[eChnlNum].
				tReportMilkData[u8TmpStationNum].ElecVal, 
				 tWNUFrameMilk.WMilkData.Conductivi,
				sizeof(tWNUFrameMilk.WMilkData.Conductivi));
	
	g_tMilkData.tMilkVal[eChnlNum].tReportMilkData[u8TmpStationNum].StationNum = u8StationNum + 1;
	
	g_tMilkData.tMilkVal[eChnlNum].u32MilkDataCurrTimeStamp = HAL_GetTick();
	
	if(GetNetworkStatus())
	{
		UploadMilkDataToSvr(eChnlNum, u8TmpStationNum);
	}
#endif
	return 0;
}

static ProtolMachine* init_uart1data_protolset(void)
{
	ProtolMachine *pack = NULL;

//	pack_info package_info = {
//			.header_length = 4, 	
//			.header = { 0x55, 0xAA, 0x55, 0x00},

//			.pack_length_location = 4,
//			.pack_length_length = 1,
//			
//			.pack_cmdno_location = 5,
//			.pack_cmdno_length = 1,
//	     
//			.pack_address_location = 6,
//			.pack_address_length = 1,
//			
//			.pack_length_as_cmdnum = false,
//			
//			.pack_data_location = 7,
//			.pack_data_length = 0,
//		
//			.pack_crc_length = 2,

//			.ender = {0xF1, -1, -1, -1},
//			.ender_length = 1,
//			
//			.rcv_timeout_timer = NULL,
//			
//			.pack_length_content = {
//				.pack_length_data_only = true
//			}

//		};

//	pack = init_protol_mach(&package_info);	

//	if (pack != NULL) {
//		pack->package_structure = NULL;
//		pack->done_callback = done_callback2;
//		
//		command_info cmd_info = {
//			.command_no = 1,
//			.command_callback = cmd_milkdata_callback
//		};
//		
//		regist_command_info(pack, cmd_info);
//		
//	} else {
////		perror ("init serial protol machine error!");
//		return NULL;
//	}

	return pack;
}

ProtolMachine* InitUart1Data(void)
{
	return init_uart1data_protolset();
}



